{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.270083Z",
     "start_time": "2024-02-21T07:35:44.259958Z"
    }
   },
   "outputs": [],
   "source": [
    "# 内置数据\n",
    "# 字典\n",
    "my_dict_1 = {'one':1, 'two':2, 'three': 3}\n",
    "my_dict_3={x:x **2 for x in range(10)}"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name1': '1111', 'name2': '2222', 'name3': '3333'}\n"
     ]
    }
   ],
   "source": [
    "list1 = ['name1', 'name2', 'name3']\n",
    "list2 = ['1111', '2222', '3333']\n",
    "my_dict_4 = dict(zip(list1, list2))\n",
    "print(my_dict_4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.270449Z",
     "start_time": "2024-02-21T07:35:44.264170Z"
    }
   },
   "id": "d68a5da1b6a23b5e",
   "execution_count": 28
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name1': '1111', 'name2': '2222', 'name3': '3333'}\n"
     ]
    }
   ],
   "source": [
    "my_dict_5 = {list1[i]:list2[i] for i in range(len(list1))}\n",
    "print(my_dict_5)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.282452Z",
     "start_time": "2024-02-21T07:35:44.267588Z"
    }
   },
   "id": "64d8803a2e29f434",
   "execution_count": 29
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name1': '1111', 'name2': '2222', 'name3': '3333'}\n"
     ]
    }
   ],
   "source": [
    "my_dict_6 = {list1[0]:list2[0], list1[1]:list2[1], list1[2]:list2[2]}\n",
    "print(my_dict_6)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.283139Z",
     "start_time": "2024-02-21T07:35:44.272702Z"
    }
   },
   "id": "bc0cc6193bad4f1c",
   "execution_count": 30
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dict_keys(['name1', 'name2', 'name3'])"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict_6.keys()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.283481Z",
     "start_time": "2024-02-21T07:35:44.276054Z"
    }
   },
   "id": "c42ec3e761cbc853",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dict_values(['1111', '2222', '3333'])"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_dict_6.values()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.303840Z",
     "start_time": "2024-02-21T07:35:44.285896Z"
    }
   },
   "id": "dd5e2d3f92572c96",
   "execution_count": 32
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1111\n"
     ]
    }
   ],
   "source": [
    "print(my_dict_6.get('name1'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-21T07:35:44.304149Z",
     "start_time": "2024-02-21T07:35:44.289030Z"
    }
   },
   "id": "9df6a2d30d17ffb7",
   "execution_count": 33
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "mail_list = {'tom':'tom@gmail.com', 'john':'john@foxmail.com', 'jerry':'jerry@163.com'}"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:31:33.098431Z",
     "start_time": "2024-02-22T06:31:33.096247Z"
    }
   },
   "id": "d00778aadc681667",
   "execution_count": 74
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "dict_values(['tom@gmail.com', 'john@foxmail.com', 'jerry@163.com'])"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mail_list.items()\n",
    "mail_list.keys()  # 键\n",
    "mail_list.values() # 值"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:31:34.654263Z",
     "start_time": "2024-02-22T06:31:34.648052Z"
    }
   },
   "id": "4d816cc90db4f58c",
   "execution_count": 75
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'tom@gmail.com'"
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mail_list.get('tom')\n",
    "mail_list['tom']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:31:37.738982Z",
     "start_time": "2024-02-22T06:31:37.735460Z"
    }
   },
   "id": "c7b3bfd89c0fafa1",
   "execution_count": 76
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom\n",
      "tom@gmail.com\n",
      "john\n",
      "john@foxmail.com\n",
      "jerry\n",
      "jerry@163.com\n"
     ]
    }
   ],
   "source": [
    "for key, value in mail_list.items():\n",
    "    print(key)\n",
    "    print(value)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:31:39.667474Z",
     "start_time": "2024-02-22T06:31:39.662627Z"
    }
   },
   "id": "12c2e6b52b261b29",
   "execution_count": 77
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'popitem(): dictionary is empty'",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[59], line 2\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[38;5;66;03m# mail = tom_mail.pop(\"tom\")\u001B[39;00m\n\u001B[0;32m----> 2\u001B[0m \u001B[43mmail_list\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpopitem\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(mail_list)\n",
      "\u001B[0;31mKeyError\u001B[0m: 'popitem(): dictionary is empty'"
     ]
    }
   ],
   "source": [
    "# mail = tom_mail.pop(\"tom\")\n",
    "mail_list.popitem()\n",
    "print(mail_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:26:39.922268Z",
     "start_time": "2024-02-22T06:26:39.910765Z"
    }
   },
   "id": "4468fa33d11c678c",
   "execution_count": 59
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom@gmail.com\n",
      "{'tom': 'tom@gmail.com', 'john': 'john@foxmail.com', 'jerry': 'jerry@163.com'}\n"
     ]
    }
   ],
   "source": [
    "# 字典默认值\n",
    "tom_mail = mail_list.setdefault('tom')\n",
    "print(tom_mail)\n",
    "print(mail_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:31:43.607611Z",
     "start_time": "2024-02-22T06:31:43.601827Z"
    }
   },
   "id": "8f5b24f30346a766",
   "execution_count": 78
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "{'tom': 'tom@gmail.com', 'john': 'john@foxmail.com', 'jerry': 'jerry@163.com', 'tom2': None}\n"
     ]
    }
   ],
   "source": [
    "tom_mail = mail_list.setdefault('tom2')\n",
    "print(tom_mail)\n",
    "print(mail_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:32:44.494552Z",
     "start_time": "2024-02-22T06:32:44.485816Z"
    }
   },
   "id": "9c2f849010b95556",
   "execution_count": 79
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc@abc.com\n",
      "{'tom': 'tom@gmail.com', 'john': 'john@foxmail.com', 'jerry': 'jerry@163.com', 'tom2': None, 'tom3': 'abc@abc.com'}\n"
     ]
    }
   ],
   "source": [
    "tom_mail = mail_list.setdefault('tom3', \"abc@abc.com\")\n",
    "print(tom_mail)\n",
    "print(mail_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:34:06.469942Z",
     "start_time": "2024-02-22T06:34:06.460897Z"
    }
   },
   "id": "9471ae8e2d4a6ab8",
   "execution_count": 80
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'tom': {'like_name': '', 'email': 'tom@gmail.com', 'phone': '13800138000'}, 'john': {'like_name': '', 'email': 'john@foxmail.com', 'phone': '13800138001'}, 'jerry': {'like_name': '', 'email': 'jerry@163.com', 'phone': '13800138002'}}\n"
     ]
    }
   ],
   "source": [
    "address_book = { 'tom': {'like_name': '', 'email': 'tom@gmail.com', 'phone': '13800138000'} , 'john': {'like_name': '', 'email': 'john@foxmail.com', 'phone': '13800138001'},  'jerry': {'like_name': '', 'email': 'jerry@163.com', 'phone': '13800138002'}}\n",
    "print(address_book)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:58:55.199249Z",
     "start_time": "2024-02-22T06:58:55.196080Z"
    }
   },
   "id": "64c30183eeb82671",
   "execution_count": 92
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'tom': {'like_name': '', 'email': 'tom@gmail.com', 'phone': '13800138000'}, 'john': {'like_name': '', 'email': 'john@foxmail.com', 'phone': '13800138001'}, 'jerry': {'like_name': '', 'email': 'jerry@163.com', 'phone': '13800138002'}, 'rose': {'like_name': '', 'email': 'rose@163.com', 'phone': '13800138003'}}\n"
     ]
    }
   ],
   "source": [
    "address_book.setdefault('rose' , {'like_name': '', 'email': 'rose@163.com', 'phone': '13800138003'})\n",
    "print(address_book)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:58:56.963857Z",
     "start_time": "2024-02-22T06:58:56.961036Z"
    }
   },
   "id": "a55ce70201255a91",
   "execution_count": 93
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'tom': {'like_name': '', 'email': 'tom@gmail.com', 'phone': '13800138000'}, 'john': {'like_name': '', 'email': 'john@foxmail.com', 'phone': '13800138001'}, 'jerry': {'like_name': '', 'email': 'jerry@163.com', 'phone': '13800138002'}}\n"
     ]
    }
   ],
   "source": [
    "address_book.pop('rose')\n",
    "print(address_book)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:59:13.874795Z",
     "start_time": "2024-02-22T06:59:13.869018Z"
    }
   },
   "id": "622ba8cb316051c8",
   "execution_count": 95
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
